package tetris;

import java.io.IOException;

import tetris.simulator.*;
import tetris.agent.*;

public class Main {

	public static void main(String[] args) {
		State sFinal = runGraphics();//runHumanPlayer();
//		State sFinal = runHumanPlayer();
		System.out.println("You have completed "+sFinal.getRowsCleared()+" rows.");
	}
	
	//run the tetris game and save image of the board at each turn, returns the final state
	public static State recordVideo()
	{
		int delay = 500;
		
		State s = new State();
		Visualizer v = new Visualizer(s);
		Agent a = new Agent();
		while(!s.hasLost()) {
			s.makeMove(a.chooseAction(s,s.legalMoves()));
			v.draw();
			v.drawNext(0,0);
			v.save(s.getTurnNumber() + ".png");
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		v.dispose();
		return s;
	}
	
	
	//runs the tetris game until the game is over and returns the final state
	public static State run()
	{
		State s = new State();
		Agent a = new Agent();
		while(!s.hasLost()) {
			s.makeMove(a.chooseAction(s,s.legalMoves()));
		}
		return s;
	}
	
	//runs and displays the tetris game until the game is over and returns the final state
	public static State runGraphics()
	{
		int maxscore =0;
		int delay = 00;
		State s = new State();;
		Visualizer v;

		v = new Visualizer();
		Agent a = new Agent();
		int population = a.ga.population*10000;
		for(int i=0;i<population;i++)
		{
			s = new State();
			v.s =s;
			a.ga.setAIValues();
			
			while(!s.hasLost()) {
				s.makeMove(a.chooseAction(s,s.legalMoves()));
//				v.draw();
//				v.drawNext(0,0);
//				try {
//					int read = System.in.read();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				try {
					Thread.sleep(delay);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			//uncomment the next line to learn, also comment out the setWeights() call in main::getscore() to update weights  
			//a.ga.sendScore(s.getRowsCleared());
			int score = s.getRowsCleared();
			if(score>maxscore)
			{
				maxscore = score;
				System.out.print("<<<<<<<<<<<<<");
				System.out.print("\nMAX SCORE" + maxscore);
				String str = a.ga.aToS(a.ga.chromosomes[a.ga.current]);
				System.out.println(str);
			}
			
		}
		v.dispose();
		return s;
	}
	
	//allows a human player to play using the 4 arrow keys
	// left: move the piece left
	// right: move the piece right
	// top: change orientation
	// down: drop piece
	// there is no time limit for choosing where to place the next piece
	public static State runHumanPlayer()
	{
		int delay = 100;
		State s = new State();
		Visualizer v = new Visualizer(s);
		v.draw();
		v.drawNext(0,0);

		
		while(!s.hasLost()) {
			s.printState();
			System.out.print("-----------------------\n");
//			int maxtop =0;
			int [] top = s.getTop();
			for(int i=0;i<top.length;i++)
//				if (top[i] > maxtop )
//					maxtop =  top[i];
//			System.out.println(s.findHoles(maxtop));
//			System.out.println(s.findBlockades());
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		v.dispose();
		return s;
	}
}
